
/*****************************************************************************
 *
 * @file:	D:\prj\git-version\BB_sensor_IMU\IMU\drivers\QMC5883L\QMC5883.h 
 * @author: Xiaodong Zhang
 * @Email:	sacntumz@foxmail.com;zhangxiaodong@beyondbit.com
 * @date:	2020/06
 * @brief:  QMC5883 driver define file

******************************************************************************
*
* Revision History:
*
* Rev.  Date        Who       		Changes
* 1     2020/06   Xiaodong Zhang  		New Created.
******************************************************************************

******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2020 BeyondBit</center></h2>
  *
  *
******************************************************************************/


#ifndef QMC5883_H
#define QMC5883_H
/******************************************************************************
 *  Include Files
 *****************************************************************************/
 #include "main.h"
 
/******************************************************************************
 *  Macro Definitions
 *****************************************************************************/
//chip id define
#ifdef S1GND
#define QMC5883_SLAVE_ADDRESS (0x1B)
#else
#define QMC5883_SLAVE_ADDRESS (0x1A)
#endif//S1GND

#define QMC5883ResetByte (0x80)//write 0x0A bit7 to 1


 //register address defines
#define QMC5883_XL_OUT	(0x00)
#define QMC5883_XH_OUT	(0x01)
#define QMC5883_YL_OUT	(0x02)
#define QMC5883_YH_OUT	(0x03)
#define QMC5883_ZL_OUT	(0x04)
#define QMC5883_ZH_OUT	(0x05)
#define QMC5883_STATUS	(0x06)
#define QMC5883_TEMP_L	(0x07)
#define QMC5883_TEMP_H	(0x08)
#define QMC5883_CTRL_1	(0x09)
#define QMC5883_CTRL_2	(0x0A)
#define QMC5883_SET_RESET_P	(0x0B)
#define QMC5883_RESERVED	(0x0C)
#define QMC5883_CHIP_ID	(0x0D)

//status bit define
#define QMC5883_DRDY_POS	(0)
#define QMC5883_OVL_POS	(1)
#define QMC5883_DOR_POS	(2)

#define QMC5883_DRDY_MASK	(1<<	QMC5883_DRDY_POS)
#define QMC5883_OVL_MASK		(1<< 	QMC5883_OVL_POS)
#define QMC5883_DOR_MASK		(1<< 	QMC5883_DOR_POS)

//#define QMC5883_CHECK_DATA_STATUS(A,MASK) ((A)&&(MASK))
// #define QMC5883_DRDY	(QMC5883_check_datastatus()&&(QMC5883_DRDAY_MASK))
// #define QMC5883_OVL		(QMC5883_check_datastatus()&&(QMC5883_OVL_MASK))
// #define QMC5883_DOR		(QMC5883_check_datastatus()&&(QMC5883_DOR_MASK))

//control register 2 bit define
typedef enum
{
	QMC5883_INT_EN		=((uint8_t)0x01),
	QMC5883_INT_DIS		=((uint8_t)0xFE),//TODO change to (~QMC5883_INT_EN)
	QMC5883_INT_IGNOR	=((uint8_t)0x55)
}QMC5883_INT_STATUS_t;

//#define QMC5883_INT_DIS			(0x0<<0)
typedef enum 
{
	QMC5883_ROLPNT_EN		=((uint8_t)0x40),
	QMC5883_ROLPNT_DIS	=((uint8_t)0xBF),//TODO change to (~QMC5883_ROLPNT_EN)
	QMC5883_ROLPNT_IGNOR		=((uint8_t)0x55)
}QMC5883_ROLPNT_STATUS_t;


//#define QMC5883_ROLPNT_DIS	(0x0<<6)
//working mode , ODR, FSR,OSR define
#define QMC5883_WORKMODE_POS	(0)
#define QMC5883_ODR_POS	(2)
#define QMC5883_FSR_POS	(4)
#define QMC5883_OSR_POS	(6)

#define QMC5883_WM_MASK		(0x03 << QMC5883_WORKMODE_POS)
#define QMC5883_ODR_MASK	(0x03 << QMC5883_ODR_POS)
#define QMC5883_FSR_MASK	(0x03 << QMC5883_FSR_POS)
#define QMC5883_OSR_MASK	(0x03 << QMC5883_OSR_POS)

#define QMC5883_WM_CLR		(~(0x03 << QMC5883_WORKMODE_POS))
#define QMC5883_ODR_CLR		(~(0x03 << QMC5883_ODR_POS))
#define QMC5883_FSR_CLR		(~(0x03 << QMC5883_FSR_POS))
#define QMC5883_OSR_CLR		(~(0x03 << QMC5883_OSR_POS))

//working mode define
typedef enum 
{
QMC5883_STANDBY	  =	((uint8_t)(0<<QMC5883_WORKMODE_POS)),
QMC5883_CONTINUE	=	((uint8_t)(1<<QMC5883_WORKMODE_POS)),
QMC5883_WM_IGNOR  =	((uint8_t)0xFF)
}QMC5883_WORKMODE_t;

//ODR define
typedef enum 
{
QMC5883_ODR_10HZ =	((uint8_t)(0<<QMC5883_ODR_POS)),
QMC5883_ODR_50HZ	= ((uint8_t)(1<<QMC5883_ODR_POS)),
QMC5883_ODR_100HZ =	((uint8_t)(2<<QMC5883_ODR_POS)),
QMC5883_ODR_200HZ =	((uint8_t)(3<<QMC5883_ODR_POS)),
QMC5883_ODR_IGNOR = ((uint8_t)0xFF)
}QMC5883_ODR_t;


//FSR define
typedef enum
{
QMC5883_FSR_2GAUSS	= ((uint8_t)(0<<QMC5883_FSR_POS)),
QMC5883_FSR_8GAUSS	= ((uint8_t)(1<<QMC5883_FSR_POS)),
QMC5883_FSR_IGNOR 	= ((uint8_t)0xFF)
}QMC5883_FSR_t;

//OSR define
typedef enum
{
	QMC5883_OSR_64	=	((uint8_t)(3<<QMC5883_OSR_POS)),
	QMC5883_OSR_128	=	((uint8_t)(2<<QMC5883_OSR_POS)),
	QMC5883_OSR_256	=	((uint8_t)(1<<QMC5883_OSR_POS)),
	QMC5883_OSR_512	=	((uint8_t)(0<<QMC5883_OSR_POS)),
	QMC5883_OSR_IGNOR = ((uint8_t)0xFF)
}QMC5883_OSR_t;


/******************************************************************************
 *  Const Definitions
 *****************************************************************************/
 
 
/*****************************************************************************
 *  Type Definitions
 *****************************************************************************/
 struct geomag_qmc5883
 {
	void *     context;
	BB_StatusTypeDef	(*read_reg)(uint8_t reg_addr,uint8_t *rdata_ptr,uint8_t rlen);
	BB_StatusTypeDef	(*write_reg)(uint8_t reg_addr,uint8_t *wdata_ptr,uint8_t wlen);
	
	struct 
	{
		QMC5883_WORKMODE_t curr_workmode;
		QMC5883_ODR_t curr_odr;
		QMC5883_FSR_t curr_fsr;
		QMC5883_OSR_t curr_osr;
		QMC5883_INT_STATUS_t curr_int;
		QMC5883_ROLPNT_STATUS_t curr_rol;
	}curr_setting;
	
 };
/*****************************************************************************
 *  Extern Variables
 *****************************************************************************/
 
 
/*****************************************************************************
 *  Function Prototypes
 *****************************************************************************/
BB_StatusTypeDef bb_qmc5883_init(void);

BB_StatusTypeDef QMC5883ReadID(uint8_t *chipid);
BB_StatusTypeDef QMC5883ChipReset(void);

// BB_StatusTypeDef QMC5883_config(QMC5883_WORKMODE_t workmode,
// 																QMC5883_ODR_t odr,
// 																QMC5883_FSR_t fsr,
// 																QMC5883_OSR_t osr,
// 																QMC5883_INT_STATUS_t int_en,
// 																QMC5883_ROLPNT_STATUS_t rol_en);

BB_StatusTypeDef QMC5883_ctrl1_config(QMC5883_WORKMODE_t workmode,
																QMC5883_ODR_t odr,
																QMC5883_FSR_t fsr,
																QMC5883_OSR_t osr);
BB_StatusTypeDef QMC5883_int_rol_config(QMC5883_INT_STATUS_t int_status,QMC5883_ROLPNT_STATUS_t rol_status);

uint8_t QMC5883_check_datastatus(void);
/**
  * @brief  QMC5883 get magnet data
	* 				- check status register and get data rdy ,overflow and data skip
	*					- read data out and formate into int16_t
  * @param:	int16_t* MagnetRawAd
  * @retval BB status
*/ 
BB_StatusTypeDef QMC5883_GetData(	int16_t* MagnetRawAd);
float	get_mag_trans_fac(void);
#endif /*QMC5883_H*/
/******************************************************************************
*
* Revision History:
*
* Rev.  YYMMDD  Who       Changes
* 1     2020/06 Xiaodong Zhang New Created.
******************************************************************************/

